package com.joe.blockview.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.joe.blockview.R;
import com.joe.blockview.model.Connection;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import androidx.annotation.Nullable;

public abstract class BlockView extends ViewGroup {
    public Paint paint = new Paint();

    public Connection connection;
    private String name = "BlockView";
    private String command = "";
    private String step = "2";

    private String pressK = "A";

    public String getPressK() {
        return pressK;
    }

    public void setPressK(String pressK) {
        this.pressK = pressK;
    }

    private View cView;

    public View getcView() {
        return cView;
    }

    protected void setcView(View view) {
        this.cView = view;
    }

    public void setStep(String step) {
        //截取前两位
        if (step.length() >= 2)
            step = step.substring(0, 2);
        this.step = step;
    }

    public String getStep() {
        return step;
    }

    //放置某些命令需要的参数
    private Map<String, String> params = new HashMap<>();

    // Current measured size of this block view.
    protected final ViewPoint mBlockViewSize = new ViewPoint();
    //自动根据内部block长度扩展
    public int nextLength = 0;

    /**
     * 获取块内部拉伸的高度
     *
     * @param tag
     * @return
     */
    public int getBlockLength(BlockView tag) {
        int length = 0;
        if (tag.getConnection().getType() == 2 || tag.getConnection().getType() == 3) {
            length = 230;
            Connection tagCon = tag.getConnection().getNextConnection();
            if (tagCon != null) {
                tag = tagCon.getTagBlockView();
                while (tag != null) {
                    length = length + getBlockLength(tag);
                    tagCon = tag.getConnection().getTagConnection();
                    if (tagCon != null) {
                        tag = tagCon.getTagBlockView();
                    } else {
                        tag = null;
                    }
                }

                length = length - 50;
            }
        } else {
            length = 130;
        }
        return length;
    }

    /**
     * 获取块内部拉伸的高度
     *
     * @return
     */
    public int getBLength() {
        Connection tag = getConnection();
        int nextLength = 0;
        if (tag != null) {
            tag = tag.getNextConnection();
            if (tag != null) {
                nextLength = nextLength + getBlockLength(tag.getTagBlockView()) - 50;
            }
            while (tag != null) {
                tag = tag.getTagConnection();
                if (tag != null) {
                    nextLength = nextLength + getBlockLength(tag.getTagBlockView());
                }
            }
        }
        return nextLength;
    }

    protected int selfWidth = 345;

    public int getSelfWidth() {
        return selfWidth;
    }

    protected int selfHeight = 250;

    public int getSelfHeight() {
        return selfHeight;
    }

    protected final Rect temp = new Rect(0, 0, 0, 0);

    public Connection getConnection() {
        return connection;
    }

    public Rect getPosition() {
        return temp;
    }

    public void setPosition(int left, int top, int right, int bottom) {
        temp.top = top;
        temp.left = left;
        temp.right = right;
        temp.bottom = bottom;
    }

    public BlockView(Context context) {
        super(context);
        init();
    }

    public BlockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BlockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    public void init() {
        setWillNotDraw(false);
        WeakReference<BlockView> blockViewWeakReference = new WeakReference<>(this);
        connection = new Connection(blockViewWeakReference);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View children = getChildAt(i);
            measureChild(children, widthMeasureSpec, heightMeasureSpec);
        }
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCommand() {
        return command;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public void setParams(String key, String value) {
        this.params.put(key, value);
    }
}
